# -*- coding: utf-8 -*-
'''
Created on 2010-03-21
Main class used for execution of summary mail preparation.

You do not specify parameters or configuration when you run the program. 
Instead you should copy settings_template.py and repositories_template.py 
to settings.py and repositories.py respectively.

@author: Jarek
'''
import os
import sys
from myutil import myutil
from shutil import rmtree
import exceptions
from datetime import datetime
import logging
from processors.main_processor import MainProcessor
from mails.mail_processor import MailProcessor
from mails.send_mail import MailSender
from processors.scanner_factory import ScannerFactory
from mails.notsending_mail_sender import NotSendingMailSender
try:
    from settings.settings import Settings #@UnresolvedImport
except ImportError:
    print "In settings dir copy settings_template.py \
        to settings.py and fill in your configuration"
    sys.exit(2)

try:
    from settings.repositories import Repositories #@UnresolvedImport
except ImportError:
    print "In settings dir copy repositories_template.py to repositories.py \
        and fill in your configuration"
    sys.exit(2)

def setup_paths(settings):
    """
    Configure all paths from settings.
    """
    storepath = settings.STORE_PATH
    if not os.path.exists(storepath):
        raise exceptions.ValueError(
            "Store path " + storepath + \
            " does not exist! Current dir " + os.path.curdir)
    if not os.path.isdir(storepath):
        raise exceptions.ValueError(
            "Store path " + storepath + \
            " is not directory! Current dir " + os.path.curdir)
    logpath = settings.LOG_PATH
    oldpath = settings.OLD_PATH
    rmtree(oldpath,True)
    if not os.path.exists(logpath):
        os.mkdir(logpath)
    os.mkdir(oldpath)
    _log_filename = os.path.join(logpath, "log_heartbeat.log")
    _log_level = logging.DEBUG if settings.DEBUG else logging.INFO
    logging.basicConfig(filename=_log_filename, level=_log_level)
    return storepath, oldpath
                    
def main():
    """
    Perform all processing.
    """
    try :
        settings = Settings()
        repositories = Repositories()
        storepath, oldpath = setup_paths(settings)
        logging.info("=" * 80)
        logging.info("Starting: " + str(datetime.now()))
        logging.info("    repositories  :" + str(Repositories.REPOSITORIES))
        logging.info("    storepath     :" + settings.STORE_PATH)
        logging.info("=" * 80)        
        mail_sender = MailSender(settings) \
            if settings.SEND_MAILS else NotSendingMailSender()
        parser = MainProcessor(settings, repositories.REPOSITORIES, \
                               MailProcessor(settings, mail_sender), \
                               ScannerFactory(settings, storepath, oldpath))
        parser.parse_and_process()
        
    except Exception as exc:
        logging.error(str(exc)) 
        raise
    finally:
        logging.info("Finished: " + str(datetime.now()))        
        
if __name__ == "__main__":
    main()
